﻿using ActionFilterPlugin.models;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;

namespace ActionFilterPlugin
{
    public class LoginHelper
    {
        IConfiguration _configuration;

        public LoginHelper(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// 用户登录后返回登录的rps
        /// </summary>
        /// <param name="userinfo">用户信息，默认是UserLoginModel</param>
        /// <returns></returns>
        public LoginResponse UserLogin<T>(T userinfo)
        {
            return UserLogin<T>(userinfo, _configuration["openkey:swagger"]);
        }

        public LoginResponse UserLogin<T>(T userinfo,string openline)
        {
            int timeOut = int.Parse(_configuration["JwtAuthorize:TimeOut"]);

            var expires = DateTime.Now.AddHours(timeOut);
            var LoginAuthModel = new LoginAuthModel[]
            {
                     new LoginAuthModel(LoginAuthEnum.UserInfo,Newtonsoft.Json.JsonConvert.SerializeObject(userinfo)),
                     new LoginAuthModel(LoginAuthEnum.Expires,expires.ToString()),
                     new LoginAuthModel(LoginAuthEnum.SourceFromKey,openline)
            };
            var key = _configuration["JwtAuthorize:BaseSecret"];

            string jwtToken = SecurityHelper.DESEncrypt(JsonConvert.SerializeObject(LoginAuthModel), key);

            return new LoginResponse()
            {
                Header = new
                {
                    alg = "DES",
                    type = "JWT"
                },
                Signature = jwtToken,
                Expires = expires,
                TokenType = _configuration["JwtAuthorize:DefaultScheme"],
                ServiceTime = DateTime.Now
            };
        }

        /// <summary>
        /// swagger测试加密数据获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="userinfo">加密的用户信息，默认是UserLoginModel</param>
        /// <returns></returns>
        public string UserSwaggerLogin<T>(T userinfo)
        {
            int timeOut = int.Parse(_configuration["JwtAuthorize:TimeOut"]);

            var expires = DateTime.Now.AddHours(timeOut);
            var LoginAuthModel = new LoginAuthModel[]
            {
                     new LoginAuthModel(LoginAuthEnum.UserInfo,Newtonsoft.Json.JsonConvert.SerializeObject(userinfo)),
                     new LoginAuthModel(LoginAuthEnum.Expires,expires.ToString()),
                     new LoginAuthModel(LoginAuthEnum.SourceFromKey,_configuration["openline:swagger"])
            };
            var key = _configuration["JwtAuthorize:BaseSecret"];

            string jwtToken = SecurityHelper.DESEncrypt(JsonConvert.SerializeObject(LoginAuthModel), key);

            LoginModel lm = new LoginModel()
            {
                Token = jwtToken,
                SourceKey = _configuration["openline:swagger"],
                dt = DateTime.Now.AddHours(1)
            };

            
            return SecurityHelper.DESEncrypt(JsonConvert.SerializeObject(lm), _configuration["JwtAuthorize:PublicSecret"]);

            
        }
    }
}
